# encoding: utf-8
require 'pp'

module SemillaGen

  TEMPLATE_FOLDER = "semilla_templates"

  # Find the template for the specified item type and template name.
  # Ex: Find a template for projects, named ClassicMVVMProjectTemplate
  #
  # @param [itemtype] can be ':class' or ':project'
  # @param [templatename] 
  def self.findTemplate(itemtype, templatename)

    dirs = [
      File.expand_path(TEMPLATE_FOLDER), #current folder
      File.expand_path("~/#{TEMPLATE_FOLDER}"), #home folder
      SemillaGen::DEFAULT_TEMPLATE_FOLDER #gem lib folder
    ]

    absolute_path = nil;
    location = File.join(itemtype.to_s, templatename.to_s)
    #puts "location #{location}"
    
    dirs.each do |dir|
      #Look for location in dir
      currentlocation = File.join(dir, location)
      #puts currentlocation
      if File.directory? currentlocation
        absolute_path = currentlocation
        break;
      end
    end

    #If we found the template, absolute_path should not be nil
    puts "Template found at: #{absolute_path}"
    return absolute_path
  end

end